package controller;

import java.io.IOException;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import restaurant.Location;

/**
 * Servlet implementation class LocationsEdit
 */
@WebServlet(urlPatterns={"/Add%20Location","Edit%20Location"})
public class LocationAddEdit extends Locations {
    private static final long serialVersionUID = 201212030048L;

    @Override
    protected Map<String, String> getLinkMap() {
        return LinkData.getAdminLinkMap();
    }

    @Override
    protected String getDispatcherPage() {
        return "/WEB-INF/location_add_edit.jsp";
    }

    @Override
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        final String idString = req.getParameter("id");
        if (null != idString && !idString.isEmpty()) {
            final EntityManager em = _emf.createEntityManager();
            Location location = em.find(Location.class, idString);
            if (null != location) {
                req.setAttribute("location", location);
            } else {
                //TODO: log error, display to user
            }

            em.close();
        }

        doGet(req, resp);
    }
}
